package com.fantasy.chance;

import java.math.BigDecimal;
import java.sql.Connection;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.HashMap;
import java.util.List;
import java.util.Map.Entry;

import com.fantasy.mysql.GetConn;
import com.fantasy.mysql.SqlRowValue;
import com.fantasy.mysql.SqlUtil;
import com.fantasy.number.ArithmeticUtil;

/**
 * @author Fantasy E-mail:vi2014@qq.com
 * @version CreateDate：2016年12月14日 下午5:04:01
 * 
 */
public class NumberChance {

	private static int position = 1;
	private static int guess = 1;

	static int chuxiancishu = 0;
	static Double gailv = 0.0;
	static Double oldGailv = 0.0;
	static int testTotal = 1000;

	static List<SqlRowValue> rows = null;

	public static void main(String[] args) {
		/*
		 * List<Integer> numberList = new ArrayList<Integer>(); for (int i = 0;
		 * i < testTotal; i++) { int x = (int) (Math.random() * 10) + 1;
		 * numberList.add(x); System.out.println(x); }
		 * calculateChance(numberList);
		 */

		calculateOne();
		//forecast();
	}

	// 预测下一期是什么
	public static void forecast() {
		int sumTol = getTotal() * 10000000;
		HashMap<String, Double> map = new HashMap<String, Double>();
		ArrayList<Double> list = new ArrayList<Double>();

		for (int i = 1; i < 11; i++) {
			double positionTol = getNumGailv(sumTol, getPositionTotal(i, "one"));
			map.put("" + i, positionTol);
			list.add(positionTol);
		}

		Collections.sort(list, new Comparator<Object>() {
			@Override
			public int compare(Object o1, Object o2) {
				return ((Double) o1).compareTo((Double) o2);
			}
		});
		
		System.out.println(list);
		for (Entry<String, Double> double1 : map.entrySet()) {
			//System.out.println(double1.getValue());
			if(double1.getValue().toString().equals(list.get(0).toString())){
				System.out.println(double1.getKey());
			}
		}

	}

	private static double getNumGailv(int sumTol, int toltal) {
		return ArithmeticUtil.strSub(ArithmeticUtil.div(toltal, sumTol, 20), ArithmeticUtil.div(toltal, sumTol + 1, 20),
				40);
	}

	// 计算某个数每次的概率
	public static void calculateOne() {
		getOneRes();
		List<Integer> numberList = new ArrayList<Integer>();
		for (SqlRowValue sqlRowValue : rows) {
			numberList.add(Integer.valueOf(sqlRowValue.getValue("one")));
		}
		calculateChance(numberList);
	}

	private static List<SqlRowValue> getQueryResult(String sql) {
		System.out.println("执行：" + sql);
		Connection conn = GetConn.getPk10Conn();
		try {
			rows = SqlUtil.getResult(conn, sql);
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		return rows;
	}

	private static void getOneRes() {
		rows = getQueryResult("select one from alldata_copy order by periods ASC");
	}

	// 这是获取某个位置出现某个数的总数
	private static int getPositionTotal(int num, String position) {
		List<SqlRowValue> res = getQueryResult(
				"select count(*) tot from alldata_copy" + " where periods < 583068 and " + position + " = " + num);
		if (res.size() == 0) {
			System.err.println("获取总数出错，方法：getPositionTotal未获得某个位置出现某个数总数");
			return 0;
		}
		int tol = Integer.valueOf(res.get(0).getValue("tot"));
		return tol;
	}

	private static int getTotal() {
		List<SqlRowValue> res = getQueryResult("select count(*) tot from alldata_copy where periods < 583068");
		if (res.size() == 0) {
			System.err.println("获取总数出错，方法：getTotal未获得总数");
			return 0;
		}
		int tol = Integer.valueOf(res.get(0).getValue("tot"));
		return tol;
	}

	private static void calculateChance(List<Integer> numberList) {
		for (int i = 0; i < numberList.size(); i++) {
			int num = numberList.get(i);
			if (num == guess) {
				chuxiancishu = chuxiancishu + 1;
				gailv = div(chuxiancishu + 0.0, (i + 1.0));
				System.out.println(
						"出现次数：" + chuxiancishu + " , 概率：" + gailv + ", 相差：" + sub(gailv, oldGailv) + ", 第" + i + "次");
				oldGailv = gailv;
			} else {

			}
		}
	}

	private static double div(Double v1, Double v2) {
		return ArithmeticUtil.div(v1, v2);
	}

	private static BigDecimal sub(Double v1, Double v2) {
		// System.out.println(v1 + ":" + v2);
		return ArithmeticUtil.sub(v1.toString(), v2.toString());
	}

}
